import { Component, Input, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { myValidNull } from '@core/util/string-utils';
import { MyModalHelper } from '@core/my-modal.helper';
import { IntegralRecordService } from '@core/routes-services/integral-record.service';
import { CompanyIntegralViewComponent } from '../../../company/integral-list/integral-view/integral-view.component';
import { RECORD_TYPE_OPTION } from '../../../../constant/dictionary';

@Component({
  selector: 'app-fans-integral',
  templateUrl: './fans-integral.component.html',
})
export class FansIntegralComponent implements OnInit {
  @Input() entity: any = {};
  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  recordTypeOption:any [] = RECORD_TYPE_OPTION
  constructor(private integralRecordService: IntegralRecordService,
              public message: NzMessageService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit(): void {

    this.initPage();
  }
  initPage() {
    this.isLoading = true;
    this.page['_filter_eq_cusId'] = this.entity.id;
    this.integralRecordService.pagination(this.page, this.keyword, null).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        console.log(this.page)
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }
  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  seeEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(CompanyIntegralViewComponent, { entity: bak }, 'lg', {
      nzTitle: '积分记录详情',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };
}
